package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.util.MoneyTextWatcher;
import com.aduerr.v1shouyin.util.ToastUtils;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/4/15 17:44
 * @change
 * @chang time
 * @class describe
 */

public class PreAuthoReceivActivity extends AppCompatActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_deposit)
    EditText etDeposit;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
    @BindView(R.id.tv_deposit_hint)
    TextView tvDepositHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autho_receiv);
        ButterKnife.bind(this);
        tvTitle.setText("收款");
        etMoney.addTextChangedListener(new MoneyTextWatcher(etMoney));
        etDeposit.addTextChangedListener(new MoneyTextWatcher(etDeposit));
    }

    @OnClick({R.id.img_back, R.id.bt_confirm, R.id.tv_deposit_hint})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.bt_confirm:
                if(TextUtils.isEmpty(etMoney.getText())) {
                    ToastUtils.showToast(PreAuthoReceivActivity.this, "请输入预授权金额");
                    return;
                }
                if(Double.parseDouble(TextUtils.isEmpty(etMoney.getText()) ? "0.0" :
                        etMoney.getText().toString())
                        == Double.parseDouble(TextUtils.isEmpty(etDeposit.getText()) ?
                        "0.0" : etDeposit.getText().toString())) {
                    ToastUtils.showToast(PreAuthoReceivActivity.this, "押金金额必须小于预授权金额");
                    return;
                }
                Intent intent = new Intent(PreAuthoReceivActivity.this,
                        ScanningActivity.class);
                intent.putExtra(Constants.QRCODE_CAPUTER, Constants.PRE_AUTHO_MONEY);
                intent.putExtra(Constants.REALMONEY, etMoney.getText().toString());
                intent.putExtra(Constants.PRE_AUTHO_PAY_MONEY,
                        etMoney.getText().toString());
                intent.putExtra(Constants.PRE_AUTHO_PAY_DEPOSIT,
                        TextUtils.isEmpty(etDeposit.getText().toString()) ? "0.0" :
                                etDeposit.getText().toString());
                intent.putExtra(Constants.PRE_AUTHO_PAY_REMARK,
                        etRemark.getText().toString());
                startActivity(intent);
                finish();
                break;
            case R.id.tv_deposit_hint:
                tvDepositHint.setVisibility(View.GONE);
                break;
        }
    }

    @OnTextChanged(value = R.id.et_money, callback =
            OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged(Editable s) {

        if(s.toString().contains(".") && s.length() - 1 - s.toString().indexOf(".") > 2)
            return;
        if(s.toString().equals(".")) return;
        if(TextUtils.isEmpty(s)) return;

        double money = Double.parseDouble(s.toString());

        if(money > 250000) {
//            ToastUtils.showToast(PreAuthoReceivActivity.this, "金额不能大于250000");
            etMoney.setText("250000");
            etMoney.setSelection(6);
            return;
        }
    }

    @OnTextChanged(value = R.id.et_deposit, callback =
            OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterDepositTextChanged(Editable s) {

        if(s.toString().contains(".") && s.length() - 1 - s.toString().indexOf(".") > 2)
            return;
        if(s.toString().equals(".")) return;
        if(TextUtils.isEmpty(s)) return;
        double deposit = Double.parseDouble(s.toString());

        if(deposit > Double.parseDouble(TextUtils.isEmpty(etMoney.getText()) ? "0.0" :
                etMoney.getText().toString())) {
            etDeposit.setText(etMoney.getText().toString());
            etDeposit.setSelection(etMoney.getText().length());
            return;
        }

    }
}
